﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Avatar.Company;
using System.Web.Security;
using Avatar.Common;
using Avatar.Person;
using AvatarHelper;
using System.Data.Linq;
using Avatar.Model;
using Avatar.MembershipService;

public partial class Company_ResumeReceived : System.Web.UI.Page
{
  public int _serviceID;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            panel_Content.Visible = false;
            using (var _adc = DBHelper.GetDataContext())
            {
                var _user = LoginManager.GetCurrentUser();
                if (_user != null)
                {
                    _serviceID = CompanyServiceManager.GetServiceType(_user.UserName).ServiceTypeID;
                    var _jobs = _adc.Job.Where(j => j.PublisherUser == _user.UserName && j.StatusID == 1).ToList();
                    
                    ListView1.DataSource = _jobs;
                    ListView1.DataBind();
                }
                if (Request.QueryString["Job"] != null)
                {
                    panel_Resume.Visible = true;

                    Guid _jobID = new Guid(Request.QueryString["Job"].ToString());

                    var _job = JobManager.GetJob(_jobID);
                    if (_job != null) lbl_JobTitle.Text = _job.Title;

                    var _records = _adc.JobApplicationRecord.Where(r => r.JobID == _jobID && r.Shielding == false).OrderByDescending(r => r.Date);
                    if (_records.Count() < 1) panel_op.Visible = false;
                    gv_ResumeList.DataSource = _records;
                    gv_ResumeList.DataBind();
                }
            }
        }
    }
    protected string DateTimeFormat(DateTime _date)
    {
        return Helper.DateTimeStringFormat(_date);
    }
    protected void ddl_JobStatusList_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (Membership.GetUser() != null)
        {
            var _jobs = JobManager.GetJobList(Membership.GetUser().UserName, Convert.ToInt32(ddl_JobStatusList.SelectedValue));
            ListView1.DataSource = _jobs;
            ListView1.DataBind();
        }
    }
    protected int GetApplicationCount(Guid _jobID)
    {
        using(var _adc=DBHelper.GetDataContext())
        {
            var _job = _adc.Job.FirstOrDefault(j=>j.ID==_jobID);
            return _job == null ? 0 : _job.JobApplicationRecord.Count();
        }
    }
    protected void cb_all_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox _sender = sender as CheckBox;
        var _check = _sender.Checked ? true : false;

        foreach (GridViewRow _row in gv_ResumeList.Rows)
        {
            foreach (Control _cell in _row.Controls)
            {
                foreach (Control _cb in _cell.Controls)
                {
                    if (_cb.GetType() == _sender.GetType())
                    {
                        ((CheckBox)_cb).Checked = _check;
                    }
                }
            }
        }
    }
    protected void btn_Favorite_Click(object sender, EventArgs e)
    {
        try
        {
            bool _ok = false;

            foreach (GridViewRow _row in gv_ResumeList.Rows)
            {
                foreach (Control _cb in _row.Cells[0].Controls)
                {
                    if (_cb.GetType().ToString() == "System.Web.UI.WebControls.CheckBox")
                    {
                        if (((CheckBox)_cb).Checked)
                        {
                            if (Membership.GetUser() != null)
                            {
                                var _company = Membership.GetUser().UserName;
                                HiddenField _lbl_ResumeID = (HiddenField)_row.Cells[0].FindControl("lbl_ResumeID");
                                string _resumeID = string.Empty;
                                if (_lbl_ResumeID != null) _resumeID = _lbl_ResumeID.Value;
                                Guid _jobID = Request.QueryString["job"] == null ? Guid.Empty : new Guid(Request.QueryString["job"].ToString());

                                _ok = ResumeFavoritesManager.AddToFavorites(_company, _resumeID, _jobID);
                                    
                            }
                        }
                    }
                }
            }
            if(_ok)
                Helper.ShowMassage("收藏简历成功！", Request.Url.ToString());
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "收藏简历出错:" + _e.Message;
        }
    }

    protected string Favorite(string _resumeID)
    {
        string _strFav = "未收藏";

        if (Membership.GetUser() != null)
        {
            var _company = Membership.GetUser().UserName;
            Guid _jobID = Request.QueryString["job"] == null ? Guid.Empty : new Guid(Request.QueryString["job"].ToString());
            var _favorite = ResumeFavoritesManager.GetFavorite(_company, _resumeID, _jobID);
            if (_favorite != null) _strFav = "已收藏";
        }

        return _strFav;
    }
    protected void btn_DeleteRecord_Click(object sender, EventArgs e)
    {
        try
        {
            bool _ok = false;

            foreach (GridViewRow _row in gv_ResumeList.Rows)
            {
                foreach (Control _cb in _row.Cells[0].Controls)
                {
                    if (_cb.GetType().ToString() == "System.Web.UI.WebControls.CheckBox")
                    {
                        if (((CheckBox)_cb).Checked)
                        {
                            if (Membership.GetUser() != null)
                            {
                                HiddenField _lbl_ResumeID = (HiddenField)_row.Cells[0].FindControl("lbl_RecordID");
                                int _recordID = _lbl_ResumeID == null ? 0 : Convert.ToInt32(_lbl_ResumeID.Value);

                                _ok = JobApplicationRecordManager.DeleteJobApplicationRecord( _recordID);

                            }
                        }
                    }
                }
            }
            if (_ok)
                Helper.ShowMassage("所选简历已忽略！", Request.Url.ToString());
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "忽略简历出错:" + _e.Message;
        }
    }
    protected void btn_Interview_Click(object sender, EventArgs e)
    {
        panel_Content.Visible = true;
    }
    protected void btn_Ok_Click(object sender, EventArgs e)
    {
        try
        {
            bool _ok = false;
            int _i = 0;
            foreach (GridViewRow _row in gv_ResumeList.Rows)
            {
                foreach (Control _cb in _row.Cells[0].Controls)
                {
                    if (_cb.GetType().ToString() == "System.Web.UI.WebControls.CheckBox")
                    {
                        if (((CheckBox)_cb).Checked)
                        {
                            _i++;
                            if (Membership.GetUser() != null)
                            {
                                var _company = Membership.GetUser().UserName;
                                HiddenField _lbl_ResumeID = (HiddenField)_row.Cells[0].FindControl("lbl_ResumeID");
                                string _resumeID = _lbl_ResumeID != null ? _lbl_ResumeID.Value : string.Empty;
                                HiddenField _lbl_JobID = (HiddenField)_row.Cells[0].FindControl("lbl_JobID");
                                Guid _jobID = _lbl_JobID != null ? new Guid(_lbl_JobID.Value) : Guid.Empty;

                                _ok = InterViewRecordManager.AddInterViewRecord(_company, _resumeID, _jobID, txt_Address.Text, txt_Content.Text);

                            }
                        }
                    }
                }
            }
            if (_ok)
                Helper.ShowMassage("发送面试通知成功！", Request.Url.ToString());
            if (_i == 0) throw new Exception("没有选择任何需要通知的简历");
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "出错:" + _e.Message;
        }
    }
}
